﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Web;
using StudentWeb.DAL;
using StudentWeb.Model;
using StudentWeb.Common;

namespace StudentWeb.BLL
{
    public class TouristRegistrationDataBO
    {
        #region 游客信息 登录 
        public IResult Login(string TouristName, string TouristPassword)
        {
            var list = new TouristRegistrationDataDAO().GetList();
            var TouristList = list.Where(item => item.TouristName == TouristName).ToList();//取对应用户名列表
            if (TouristList.Count < 1)//如该用户记录不存在 
            {
                return new Result(ResultCode.Fail, "用户名不存在");
            }
            if (TouristList[0].TouristPassword != TouristPassword)//判断密码 
            {
                return new Result(ResultCode.Fail, "密码错误");
            }
            return new Result(ResultCode.Success, "登录成功");
        }
        #endregion
        #region 游客信息 注册 
        public bool SaveTouristInfo(dynamic data)
        {
            int TouristID = data.TouristID ?? 0;
            var result = 0;
            if (TouristID > 0)
            {
                var record = new TouristRegistrationDataDAO().GetInfo(TouristID);
                record.TouristName = data.TouristName;
                record.TouristPassword = data.TouristPassword;
                record.TouristPhone = data.TouristPhone;
                record.RegistrationDate = data.RegistrationDate;
                record.Gender = data.Gender;
                record.BirthDate = data.BirthDate;
                result = new TouristRegistrationDataDAO().Update(record);
            }
            else if (TouristID == 0)
            {
                var user = new TouristRegistrationData()
                {
                    TouristName = data.TouristName,
                    TouristPassword = data.TouristPassword,
                };
                result = new TouristRegistrationDataDAO().Insert(user);
            }
            return result > 0;
        }
        #endregion
        #region 游客信息 全部记录
        public IResult GetTouristList()
        {
            var list = new TouristRegistrationDataDAO().GetList();
            return new DataResult(list);
        }
        #endregion
        #region 游客信息 单条记录
        public IResult GetTouristRegistrationInfo(int TouristID)
        {
            var record = new TouristRegistrationDataDAO().GetInfo(TouristID);
            return new DataResult(record);
        }
        #endregion
        #region 游客信息 添加
        public bool InsertTouristInfo(dynamic data)
        {
            var user = new TouristRegistrationData()
            {
                TouristName = data.TouristName,
                TouristPassword = data.TouristPassword,
                TouristPhone = data.TouristPhone,
                RegistrationDate = data.RegistrationDate,
                Gender = data.Gender,
                BirthDate = data.BirthDate,
            };
            var result = new TouristRegistrationDataDAO().Insert(user);
            return result > 0;
        }
        #endregion
        #region 游客信息 修改
        public bool UpdateTouristInfo(dynamic data)
        {
            int TouristID = data.TouristID;
            var record = new TouristRegistrationDataDAO().GetInfo(TouristID);
            record.TouristName = data.TouristName;
            record.TouristPassword = data.TouristPassword;
            record.TouristPhone = data.TouristPhone;
            record.RegistrationDate = data.RegistrationDate;
            record.Gender = data.Gender;
            record.BirthDate = data.BirthDate;
            var result = new TouristRegistrationDataDAO().Update(record);
            return result > 0;
        }
        #endregion
        #region 游客信息 删除
        public IResult DeleteTouristInfo(int TouristID)
        {
            var res = new TouristRegistrationDataDAO().Delete(TouristID);
            if (res > 0)
            {
                return new Result(ResultCode.Success, "删除成功");
            }
            else
            {
                return new Result(ResultCode.Fail, "删除失败");
            }
        }
        #endregion
        #region 管理员信息 分页列表
        public IResult PageTouristList(int pageIndex, int pageSize, string wc = null)
        {
            var list = new TouristRegistrationDataDAO().GetList(pageIndex, pageSize, wc);
            return new DataResult(list);
        }
        #endregion
    }
}
